package com.chang.multithread.ThreadPool;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicIntegerArray;

//创建固定大小的线程池
//救急线程数=最大线程数目-核心线程数  这个方法没有救急线程
@Slf4j
public class FixThreadPool {
    public static void main(String[] args) {
        ExecutorService pool = Executors.newFixedThreadPool(2, new ThreadFactory() {
            private AtomicInteger t=new AtomicInteger(1);//设置原子整形初始值
            @Override
            public Thread newThread(Runnable r) {  //自定义线程名字
                return new Thread(r,"mypool_t"+t.getAndIncrement());
            }
        });//使用线程工厂创建线程，并且为非守护线程
        pool.execute(()->{
            log.debug("任务1");
        });
        pool.execute(()->{
            log.debug("任务2");
        });
        pool.execute(()->{
            log.debug("任务3");
        });
        pool.execute(()->{
            log.debug("任务4");
        });
    }
}
